package com.lagou.controller;

import com.github.pagehelper.PageInfo;
import com.lagou.domain.ResponseResult;
import com.lagou.domain.Role;
import com.lagou.domain.User;
import com.lagou.domain.UserVo;
import com.lagou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/findAllUserByPage")
    public ResponseResult findAllUserByPage(@RequestBody UserVo uservo) {

        PageInfo<User> pageInfo = userService.findAllUserByPage(uservo);

        ResponseResult result = new ResponseResult(true, 200, "查询用户信息成功", pageInfo);

        return result;
    }

    @RequestMapping("/login")
    public ResponseResult login(User user, HttpServletRequest request) throws Exception {

        User login = userService.login(user);

        if (null != login) {

            HttpSession session = request.getSession();
            String accessToken = UUID.randomUUID().toString();
            session.setAttribute("access_token", accessToken);
            session.setAttribute("user_id", login.getId());

            HashMap<String, Object> map = new HashMap<>();
            map.put("access_token", accessToken);
            map.put("user_id", login.getId());
            map.put("user", login);

            return new ResponseResult(true, 1, "登录成功", map);
        }

        return new ResponseResult(true, 400, "用户名或密码错误", null);
    }

    @RequestMapping("/findUserRelationRoleById")
    public ResponseResult findUserRelationRoleById(Integer id) {

        List<Role> relationRoleById = userService.findUserRelationRoleById(id);

        ResponseResult result = new ResponseResult(true, 200, "查询用户角色成功", relationRoleById);

        return result;
    }

    @RequestMapping("/userContextRole")
    public ResponseResult userContextRole(@RequestBody UserVo userVo) {

        userService.userContextRole(userVo);

        return new ResponseResult(true, 200, "分配角色成功", null);
    }

    @RequestMapping("/updateUserStatus")
    public ResponseResult updateUserStatus(User user) {

        userService.updateUserStatus(user);

        return new ResponseResult(true, 200, "响应成功", user.getStatus());
    }

    @RequestMapping("/getUserPermissions")
    public ResponseResult getUserPermissions(HttpServletRequest request) {

        String headerToken= request.getHeader("Authorization");

        String accessToken = (String) request.getSession().getAttribute("access_token");

        if (headerToken.equals(accessToken)) {
            Integer userId = (Integer) request.getSession().getAttribute("user_id");

            return userService.getUserPermissions(userId);
        }

        return new ResponseResult(false, 400, "获取菜单信息失败", null);
    }
}
